package qj.tool.test;

import java.util.ArrayList;
import java.util.List;

import qj.util.StringUtil4;

/**
 * Created by QuanLA Date: Mar 24, 2006 Time: 10:45:32 AM
 */
public class TimeStepper {
	private long lastLong = 0;
	
	private boolean reportImm = true;

	private int initStep = 0;

	private int step = initStep;

	private boolean reverse = false;

	private boolean pause = false;
	
	private List list = new ArrayList();

	public TimeStepper() {
		step();
	}
	
	public void reverseFrom(int from) {
		initStep = from + 1;
		step = initStep;
		reverse = true;
	}

	public void reset() {
		step = initStep;
	}

	public void pause() {
		pause = true;
	}

	public void resume() {
		pause = false;
	}

	public void step() {
		if (!pause) {
			long currentTimeMillis = System.currentTimeMillis();
			if (step != initStep) {
				String message = "" +
				 "[REPORT]" + StringUtil4.ensureLengthLeft("step" + step, 8) +
				 ":" +
						 StringUtil4.ensureLengthRight("" +
						(currentTimeMillis - lastLong)
						 , 6)
				;
				
				if (!reportImm)
					list.add(message);
				else
					System.out.println(message);
			}
			lastLong = currentTimeMillis;
			if (!reverse)
				step++;
			else
				step--;
		}
	}

	public void report() {
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}
	}

	public void setReportImm(boolean reportImm) {
		this.reportImm = reportImm;
	}

	public void message(String message) {
		if (!reportImm)
			list.add(message);
		else
			System.out.println(message);
	}
}


